<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use app\index\model\User as UserModel;
class User extends Controller
{
	private static $codeNumber = 100;
	private $time = 0;
    public function login(){
        return view();
	}
	public function personal(){
		return view();
	}
	public function nameLogin(Request $request){
		if ($request->isPost()) {
			$user = new UserModel;

			$username =addslashes(trim($_POST['username']));
			$password = md5(addslashes(trim($_POST['password'])));

			$nameId = $user->get(['username'=>$username])->id;
			$passId = $user->get(['password'=>$password])->id;
			if (empty($username) || empty($password)) {
				echo json_encode(['code'=>1,'message'=>'用户名及密码不能为空']);die;
			}
			if (empty($nameId)) {
				echo json_encode(['code'=>1,'message'=>'用户名不存在']);
				die;
			}
			if (empty($passId)) {
				echo json_encode(['code'=>1,'message'=>'密码不正确']);
				die;
			}
			if ($nameId === $passId) {
				Session::set('username',$username);				
				echo json_encode(['code'=>2,'message'=>'验证通过']);			
			}
		}
		
	}
	public function smsRegister(Request $request){
		if ($request->isPost()) {
			$phoneNumber = addslashes(trim($_POST['number']));

			if (!empty($phoneNumber)) {
				$codeNumber = mt_rand(100000,999999);
				$startTime = time();
				echo $codeNumber;
				Session::set('codeNumber',$codeNumber);
				Session::set('startTime',$startTime);
		
			}else{
				echo json_encode(['code'=>1,'message'=>'手机号未填写']);
				die;
			}
		}
			
    }
    public function smsLogin(Request $request){
		// $UserModel = new UserModel;
		// $user = $UserModel->get(['mobile'=>'15120057942']);
		// echo $user->username;
		if ($request->isPost()) {
			$phoneNumber = addslashes(trim($_POST['number']));
			Session::set('phoneNumber',$phoneNumber);

			if (!empty($phoneNumber)) {
				$UserModel = new UserModel;			
				$result = $UserModel->get(['mobile'=>$phoneNumber]);
				
				if ($result) {
					$codeNumber = mt_rand(100000,999999);
					$startTime = time();
					echo $codeNumber;
					Session::set('codeNumber',$codeNumber);
					Session::set('startTime',$startTime);
				}else{
					echo json_encode(['code'=>1,'message'=>'用户不存在']);
					die;
				}		
			}
		}
		
    }
    //两次请求无关
    public function vcode(Request $request){
		if ($request->isPost()) {
			$vcode=$request -> session('codeNumber');
			$start_time = $request -> session('startTime');
			$code = $_POST['code'];
			if (empty($code)) {
				$arr = ['message'=>'验证未填写','code'=>1];			
				echo json_encode($arr);
			}
			$end_time = time(); 
			$time_dif = $end_time-$start_time;
			// echo json_encode(['end_time'=>$end_time,'start_time'=>$start_time,'time_dif'=>$time_dif]);
			if ($time_dif < 60) {
				if (!empty($code) && $code === $vcode) {
					echo json_encode(['code'=>2,'message'=>'验证通过']);
				}
			}else{
				echo json_encode(['code'=>1,'message'=>'超时请重新发送验证码']);die;
			}
		}
		
    }
    public function forgetPass(){
    	return view();
	}
	public function newPass(){
		return view();
	}
	public function checkNewPass(Request $request){
		if ($request->isPost()) {
			$phoneNumber = $request->session('phoneNumber');
			$user = new UserModel;
			// $phoneNumber = '12345678910';
			$result = $user->get(['mobile'=>$phoneNumber]);
			if ($request) {
				$password = md5(addslashes(trim($_POST['password'])));
				$v_password = md5(addslashes(trim($_POST['v_password'])));
				if (!empty($password) && !empty($v_password) && empty($password)===empty($v_password)) {
					$res=$user->save([
						'password'=>$password
					],['mobile'=>$phoneNumber]);
					if ($res) {
						echo json_encode(['code'=>2,'message'=>'验证通过']);
					}

				}else{
					echo json_encode(['code'=>1,'message'=>'信息不完整']);die;
				}
			}else{
				echo json_encode(['code'=>1,'message'=>'用户不存在']);
				die;
			}
		}
		
		
	}
    public function register(){
    	return view();
	}
	public function addUser(Request $request){
		if ($request->isPost()) {
			$username = addslashes(trim($_POST['username']));
			$password = md5(addslashes(trim($_POST['password'])));		
			$v_password = md5(addslashes(trim($_POST['v_password'])));
			$phoneNumber = addslashes(trim($_POST['phoneNumber']));
			
					
			if (empty($username) || empty($password) || empty($v_password) || empty($phoneNumber)) {
				echo json_encode(['code'=>1,'message'=>'信息不完整']);
				die;
			}
			$user = new UserModel;
			if($user->get('mobile')){
				echo json_encode(['code'=>1,'message'=>'该号码已存在']);
				die;
			}		
			$user->data([
				'username'=>$username,
				'password'=>$password,
				'mobile'=>$phoneNumber
			]);
			$result = $user->save();
			if ($result) {
				echo json_encode(['code'=>2,'message'=>'注册成功']);
			}else{
				echo json_encode(['code'=>1,'message'=>'注册失败']);
				die;
			}
		}
		
	}
	public function checkLogin(Request $request){
		if ($request->isGet()) {
			$username = $request->session('username');
			$phoneNumber = $request->session('phoneNumber');
			if (isset($username) || isset($phoneNumber)) {
				echo json_encode(['code'=>2,'message'=>'用户已登录']);
			}else{
				echo json_encode(['code'=>1,'message'=>'用户未登录']);
				die;
			}
		}
		
	}
	public function logout(Request $request){

		if($request->isGet()){
			Session::clear();			
			$res=$request->session();
			if ($res) {
				echo json_encode(['success'=>false]);
				die;
			}else{
				echo json_encode(['success'=>true]);				
			}
		}				
	}
}
